package com.zhou.base.flowable7.test.config.idm.impl;

import cn.hutool.core.collection.CollUtil;
import com.zhou.base.flowable7.test.config.idm.entity.CustomGroupDetails;
import org.flowable.common.engine.impl.interceptor.CommandContext;
import org.flowable.idm.api.Group;
import org.flowable.idm.engine.impl.GroupQueryImpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;

/**
 * @author xianfeng
 * @description:
 * @date 2024/1/11 19:01
 */
public class CustomGroupQueryImpl extends GroupQueryImpl {

    private static final Logger logger = LoggerFactory.getLogger(CustomGroupQueryImpl.class);

    private static final long serialVersionUID = 1L;


    @Override
    public long executeCount(CommandContext commandContext) {
        return executeQuery().size();
    }

    @Override
    public List<Group> executeList(CommandContext commandContext) {
        return executeQuery();
    }

    protected List<Group> executeQuery() {
        if (getUserId() != null) {
            return findGroupsByUser(getUserId());
        } else if (getId() != null) {
            return findGroupsById(getId());
        } else {
            return findAllGroups();
        }
    }

    protected List<Group> findGroupsByUser(String userId) {
        //todo 1. 从缓存中获取
        //todo 2. 从数据库和远程获取，并缓存
        if (userId.equals("zxf")) {
            logger.info("获取zxf的组织");
            //可以获取用户的部门和角色都设置为组
            CustomGroupDetails groupDetails = new CustomGroupDetails("management", "管理组织", "22");
            return CollUtil.newArrayList(groupDetails);
        }
        return CollUtil.newArrayList();
    }

    protected List<Group> findGroupsById(String id) {
        return null;
    }

    protected List<Group> findAllGroups() {
        return null;
    }
}
